<?php

/**
 * This is the model class for table "tbl_order".
 *
 * The followings are the available columns in table 'tbl_order':
 * @property integer $id
 * @property string $datetime
 * @property integer $status_id
 * @property integer $customer_id
 * @property integer $shipment_id
 * @property string $totalprice
 *
 * The followings are the available model relations:
 * @property Customer $customer
 * @property Shipment $shipment
 * @property Status $status
 * @property Product[] $tblProducts
 */
class Order extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Order the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_order';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            //array('datetime, status_id, customer_id, shipment_id', 'required'),
            array('status_id, customer_id, shipment_id', 'numerical', 'integerOnly' => true),
            array('totalprice', 'length', 'max' => 16),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, datetime, status_id, customer_id, shipment_id, totalprice', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'users' => array(self::BELONGS_TO, 'users', 'customer_id'),
            'shipment' => array(self::BELONGS_TO, 'Shipment', 'shipment_id'),
            'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
            'orderproduct' => array(self::MANY_MANY, 'OrderProduct', 'tbl_order_product(order_id, product_id)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'datetime' => 'Thời điểm đặt hàng',
            'status_id' => 'Trạng thái',
            'customer_id' => 'Khách hàng',
            'shipment_id' => 'Phương thức vận chuyển',
            'totalprice' => 'Thành tiền',
        );
    }

    /**
     * This is invoked before the record is saved.
     * @return boolean whether the record should be saved.
     */
    protected function beforeSave() {
        if (parent::beforeSave()) {

            $this->datetime = date('Y-m-d H:i:s');
            $this->status_id = '1';
            $this->customer_id = Yii::app()->user->id;
            $this->totalprice = $_SESSION['price'];           

            return true;
        }
        else
            return false;
    }

    /**
     * This is invoked after the record is saved.
     * @return boolean whether the record should be saved.
     */
    protected function afterSave() {
        $connection = Yii::app()->db;
        $sql = "insert into tbl_order_product
                    select tbl_order.id,product_id,amount
                    from tbl_cart, tbl_order
                    where tbl_cart.customer_id = tbl_order.customer_id
                    and tbl_cart.owner='" . Yii::app()->User->getState('owner') . "'
                    and tbl_order.id = (select max(id) from tbl_order)";
        $command = $connection->createCommand($sql);
        $command->execute();
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('datetime', $this->datetime, true);
        $criteria->compare('status_id', $this->status_id);
        $criteria->compare('customer_id', $this->customer_id);
        $criteria->compare('shipment_id', $this->shipment_id);
        $criteria->compare('totalprice', $this->totalprice, true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

}